package top.mccat.web.controller.employee;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mccat.common.annotation.Log;
import top.mccat.common.core.controller.BaseController;
import top.mccat.common.core.domain.AjaxResult;
import top.mccat.common.core.domain.entity.SysUser;
import top.mccat.common.enums.BusinessType;
import top.mccat.common.utils.poi.ExcelUtil;
import top.mccat.common.core.page.TableDataInfo;
import top.mccat.system.domain.EmployeeExcellent;
import top.mccat.system.domain.EmployeePunch;
import top.mccat.system.entity.ExcellentEntity;
import top.mccat.system.entity.PunchEntity;
import top.mccat.system.service.IEmployeeExcellentService;
import top.mccat.system.service.ISysUserService;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-30
 */
@RestController
@RequestMapping("/system/excellent")
public class EmployeeExcellentController extends BaseController
{
    @Autowired
    private IEmployeeExcellentService employeeExcellentService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询【优秀员工评审】列表
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmployeeExcellent employeeExcellent)
    {
        startPage();
        List<EmployeeExcellent> list = employeeExcellentService.selectEmployeeExcellentList(employeeExcellent);
        List<ExcellentEntity> excellentEntities = new ArrayList<>();
        for(EmployeeExcellent excellent : list){
            SysUser sysUser = sysUserService.selectUserById(excellent.getUserId());
            ExcellentEntity excellentEntity = new ExcellentEntity(excellent,sysUser);
            excellentEntities.add(excellentEntity);
        }
        return getDataTable(excellentEntities);
    }

    /**
     * 导出【优秀员工评审】列表
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:export')")
    @Log(title = "【优秀员工评审】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmployeeExcellent employeeExcellent)
    {
        List<EmployeeExcellent> list = employeeExcellentService.selectEmployeeExcellentList(employeeExcellent);
        ExcelUtil<EmployeeExcellent> util = new ExcelUtil<EmployeeExcellent>(EmployeeExcellent.class);
        util.exportExcel(response, list, "【优秀员工评审】数据");
    }

    /**
     * 获取【优秀员工评审】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(employeeExcellentService.selectEmployeeExcellentById(id));
    }

    /**
     * 新增【优秀员工评审】
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:add')")
    @Log(title = "【优秀员工评审】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmployeeExcellent employeeExcellent)
    {
        if(employeeExcellentService.selectEmployeeNowMonth(employeeExcellent.getUserId())!=null){
            return AjaxResult.error(AjaxResult.StatusCode.HaveAskError.getCode(), "错误！该员工本月已被评为优秀员工！");
        }
        return toAjax(employeeExcellentService.insertEmployeeExcellent(employeeExcellent));
    }

    /**
     * 修改【优秀员工评审】
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:edit')")
    @Log(title = "【优秀员工评审】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmployeeExcellent employeeExcellent)
    {
        return toAjax(employeeExcellentService.updateEmployeeExcellent(employeeExcellent));
    }

    /**
     * 删除【优秀员工评审】
     */
    @PreAuthorize("@ss.hasPermi('system:excellent:remove')")
    @Log(title = "【优秀员工评审】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(employeeExcellentService.deleteEmployeeExcellentByIds(ids));
    }
}
